﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutForEUI.cshtml";
}
<table id="Email_Index_grid" class="easyui-datagrid" style="width:100%;" title="系统邮箱" data-options="
       rownumbers:true,
       singleSelect:true,
       autoRowHeight:false,
       pagination:true,
       fitColumns:true,
       fit:true,
       striped:true,
       checkOnSelect:false,
       selectOnCheck:false,
       toolbar:'#Email_Index_tb',
       collapsible:false,
       pageSize: @SGArchiveSystem7x.Models.CCommonsModels.PageSize,
       pageList:[10,15,20]">
    <thead frozen="true">
        <tr>
            <th field="E_TYPE" width="200" align="center" formatter="formatType">邮箱类型</th>
            <th field="E_EMAIL" width="200" align="center">邮箱地址</th>
            <th field="E_USER" width="200" align="center">邮箱服务器</th>
            <th field="E_PWD" width="200" align="center" formatter="formatPwd">邮箱密码</th>
            <th field="E_ID" width="80" align="center" formatter="formatEdit">编辑</th>
            <th field="E_ID2" width="80" align="center" formatter="formatDel">删除</th>
        </tr>
    </thead>
</table>
<div id="Email_Index_tb" style="padding:2px 5px;">
    <a href="javascript:void(0);" id="Email_Index_btnAdd" class="easyui-linkbutton button-blue" iconCls="icon-add" onclick="showAdd()" style="width:110px;height:30px;">添加邮箱</a>
</div>
<div id="Email_Index_win" class="easyui-window" title="编辑邮箱" data-options="modal:true,closed:true,iconCls:'icon-save'" minimizable="false" maximizable="false" resizable="false" collapsible="false"
     style="width:400px;height:330px;padding:10px">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center'" style="padding:10px;border:0px">
            <div style="padding-bottom:10px">
                <select class="easyui-combobox" id="Email_Index_cmbType" label="邮箱类型:" labelwidth="80px" labelalign="right" style="width:100%" editable="false" panelHeight="auto">
                    <option value="0">审批接收</option>
                    <option value="1">信息发送</option>
                </select>
            </div>
            <div style="padding-bottom:10px">
                <input class="easyui-textbox" id="Email_Index_txtEmail" label="邮箱地址:" labelwidth="80px" labelalign="right" style="width:100%" data-options="required:true"/>
            </div>
            <div style="padding-bottom:10px">
                <input class="easyui-textbox" id="Email_Index_txtServer" label="邮箱服务器:" labelwidth="80px" labelalign="right" style="width:100%" data-options="required:true" />
            </div>
            <div style="padding-bottom:10px">
                <input class="easyui-textbox" id="Email_Index_txtPwd" label="邮箱密码:" labelwidth="80px" labelalign="right" style="width:100%" />
            </div>
        </div>
        <div data-options="region:'south',border:false" style="text-align:right;padding:0px,0,0;">
            <a class="easyui-linkbutton button-blue" data-options="iconCls:'icon-ok'" href="javascript:void(0)" style="width:100px" onclick="save()" id="Email_Index_btnAdd">添加</a>
            <a class="easyui-linkbutton button-blue" data-options="iconCls:'icon-ok'" href="javascript:void(0)" style="width:100px" onclick="save()" id="Email_Index_btnModify">修改</a>
        </div>
    </div>
</div>
<script>
    var nowid = 0;
    $(document).ready(function () {
        $('#Email_Index_grid').datagrid({
            url: '/Email/Get_ajaxEmailList',
            singleSelect: true,
            method: 'post',
            onLoadSuccess: function (data) {
                $('.pagination-page-list').hide();
            }
        });

        InitPagenation();
    });

    ///初始化分页
    function InitPagenation() {
        //设置分页控件
        var p = $('#Email_Index_grid').datagrid('getPager');
        $(p).pagination({
            onSelectPage: function (pageNumber, pageSize) {
                QueryPager(pageNumber, pageSize);
            }
        });
    }

    ///刷新之后，把存放数据清除
    function UpdateList() {
        QueryPager($('#Email_Index_grid').datagrid('options').pageNumber, $('#Email_Index_grid').datagrid('options').pageSize);
    }


    ///分页查询
    function QueryPager(num, size) {
        $('#Email_Index_grid').datagrid('options').url = '/Email/Get_ajaxEmailListPager?num=' + num + '&size=' + size;
        $('#Email_Index_grid').datagrid('reload');
        $('#Email_Index_grid').datagrid('options').pageNumber = num;
        $('#Email_Index_grid').datagrid('options').pageSize = size;
        return;
    } 

    function showAdd() {
        nowid = 0;
        $('#Email_Index_txtEmail').textbox('setValue', '');
        $('#Email_Index_txtPwd').textbox('setValue', '');
        $('#Email_Index_txtServer').textbox('setValue', '');
        document.getElementById('Email_Index_btnAdd').style.display = '';
        document.getElementById('Email_Index_btnModify').style.display = 'none';
        $('#Email_Index_win').window('open');
    }

    function showEdit(id) {
        nowid = id;
        document.getElementById('Email_Index_btnModify').style.display = '';
        document.getElementById('Email_Index_btnAdd').style.display = 'none';
        $.ajax({
            url: "/Email/Get_ajaxEmail",
            data: { id: nowid },
            dataType: "json",
            type: "post",
            success: function (value) {
                $('#Email_Index_txtEmail').textbox('setValue', value.en.E_EMAIL);
                $('#Email_Index_txtPwd').textbox('setValue', value.en.E_PWD);
                $('#Email_Index_txtServer').textbox('setValue', value.en.E_USER);
                $('#Email_Index_cmbType').combobox('setValue', value.en.E_TYPE);
                $('#Email_Index_win').window('open');
            }
        })
        
    }

    function save() {
        var name = $('#Email_Index_txtEmail').val();
        var type = $('#Email_Index_cmbType').combobox('getValue');

        if (type == 1) {
            var data = $('#Email_Index_grid').datagrid('getData');
            var len = data.rows.length;
            for (var i = 0; i < len; i++) {
                if (data.rows[i].E_TYPE == 1) {
                        $.messager.alert('提示', '只能设定一个信息发送邮箱', 'error');
                        return;
                }
            }
        }

        var pwd = $('#Email_Index_txtPwd').val();
        var server = $('#Email_Index_txtServer').val();

        if (name == '') {
            $.messager.alert('提示', '邮箱名不能为空!', 'error');
            return;
        }

        if (type == 1) {
            if (pwd == '') {
                $.messager.alert('提示', '信息发送类邮箱必须设定密码!', 'error');
                return;
            }
        }

         $.ajax({
            url: "/Email/Set_ajaxAddEmail",
            data: { id: nowid, type: type, name: name,server:server, pwd: pwd },
            dataType: "text",
            type: "post",
            success: function (value) {
                if (value == "1") {
                    $.messager.alert('提示', '保存成功!', 'info');
                    $('#Email_Index_win').window('close');
                    UpdateList();
                }
                else if (value == '0')
                    $.messager.alert('提示', '邮箱名重复!', 'error');
                else 
                    $.messager.alert('提示', '保存失败!', 'error');
            }
        })
    }

    function Del(id) {
        alert(id);
        $.messager.confirm('提示', '确定要删除选择的邮箱吗?', function (r) {
            if (r) {
                $.ajax({
                    url: "/Email/Set_ajaxDelEmail",
                    data: { id: id },
                    dataType: "text",
                    type: "post",
                    success: function (value) {
                        if (value == "1") {
                            $.messager.alert('提示', '删除成功!', 'info');
                            UpdateList();
                        }
                        else
                            $.messager.alert('提示', '保存失败!', 'error');
                    }
                })
            }
        });
        
    }

    function formatPwd(value, row) {
        if (value != null && value != "")
            return "<span>***********</span>";
    }

    function formatType(value, row) {
        if (value == '0')
            return "<span>信息发送</sapn>";
        else if (value == '1')
            return "<span>审批接收</span>";
    }

    function formatEdit(value, row) {
        return "<a href='javascript:void(0)' onclick='showEdit(" + value + ")'><img src='../Content/images/pencil.png' width='16px' height='16px' />&nbsp;编辑</a>"
    }

    function formatDel(value, row) {
        var id = row["E_ID"];
        return "<a href='javascript:void(0)' onclick='Del(" + id + ")'><img src='../Content/images/close.png' width='16px' height='16px' />&nbsp;删除</a>"
    }
</script>

